package trying.Design;

import java.beans.JavaBean;
import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        RewardService rewardService = new RewardService();
        rewardService.issueReward("Waimai","aaaa");
    }
}

class RewardService {
    public void issueReward(String rewardType, Object ... params) throws ClassNotFoundException {
        Strategy strategy = StrategyContext.getStrategy(rewardType);
        if (strategy == null) {
            Class.forName("trying.Design.Waimai");
        }
        strategy.issue(params);
    }
}
// 策略上下文，用于管理策略的注册和获取
class StrategyContext {
    private static final Map<String, Strategy> registerMap = new HashMap<>();
    // 注册策略
    public static void registerStrategy(String rewardType, Strategy strategy) {
        registerMap.putIfAbsent(rewardType, strategy);
    }
    // 获取策略
    public static Strategy getStrategy(String rewardType) {
        Strategy strategy = registerMap.get(rewardType);
        return strategy;
    }
}
// 抽象策略类
abstract class AbstractStrategy implements Strategy {
    // 类注册方法
    public void register() {
        StrategyContext.registerStrategy(getClass().getSimpleName(), this);
    }
}
// 单例外卖策略
class Waimai extends AbstractStrategy implements Strategy {
    private static final Waimai instance = new Waimai();
    private Waimai() {
        register();
    }
    public static Waimai getInstance() {
        return instance;
    }
    public void issue(Object... params) {
        System.out.println("Waimai");
    }
}
// 单例酒旅策略
class Hotel extends AbstractStrategy implements Strategy {
    private static final Hotel instance = new Hotel();
    private Hotel() {
        register();
    }
    public static Hotel getInstance() {
        return instance;
    }

    public void issue(Object... params) {
        System.out.println("Hotel");
    }
}
// 单例美食策略
class Food extends AbstractStrategy implements Strategy {
    private static final Food instance = new Food();
    private Food() {
        register();
    }
    public static Food getInstance() {
        return instance;
    }
    public void issue(Object... params) {
        System.out.println("Food");
    }
}

// 策略接口
interface Strategy {
    void issue(Object ... params);
}